package com.situ.music.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.situ.music.R;
import com.situ.music.activity.MainActivity;
import com.situ.music.activity.MusicPlayAcitvity;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

public class FileAdapter extends BaseAdapter {
    private MainActivity mainActivity;
    private File[] files;

    public FileAdapter(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
    }

    @Override
    public int getCount() {
        return files.length;
    }

    @Override
    public Object getItem(int position) {
        return files[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // getView这个方法是渲染ListView的每一条  Data->View进行绑定
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHoder viewHoder = new ViewHoder();
        if (convertView == null) {// 第一次
            LayoutInflater layoutInflater = (LayoutInflater) mainActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_item, null);
            viewHoder.ivFileIcon = convertView.findViewById(R.id.iv_file_icon);
            viewHoder.tvFileName = convertView.findViewById(R.id.tv_file_name);
            convertView.setTag(viewHoder);
        } else {
            viewHoder = (ViewHoder) convertView.getTag();
        }

        File file = files[position];
        String fileName = file.getName();
        viewHoder.tvFileName.setText(fileName);
        if (file.isDirectory()) {
            viewHoder.ivFileIcon.setBackgroundResource(R.drawable.icon_folder);
        } else { // 都是文件
            // android.mp3
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);// mp3、mp4
            if ("mp3".equalsIgnoreCase(suffix)){
                viewHoder.ivFileIcon.setBackgroundResource(R.drawable.icon_audio);
            } else if ("mp4".equalsIgnoreCase(suffix)) {
                viewHoder.ivFileIcon.setBackgroundResource(R.drawable.icon_video);
            } else {
                viewHoder.ivFileIcon.setBackgroundResource(R.drawable.icon_file);
            }
        }

        // 设置点击事件
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (file.isDirectory()) {
                    // 更新最新的当前目录
                    mainActivity.updateCurrentPath(file);
                    updateData(file);
                } else {
                    String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);// mp3、mp4
                    if ("mp3".equalsIgnoreCase(suffix)){
                        Intent intent = new Intent(mainActivity, MusicPlayAcitvity.class);
                        intent.putExtra("path", file.getAbsolutePath());
                        // Intent除了可以传递字符串，还可以传递ArrayList集合
                        ArrayList<String> mp3List = getMp3Files(files);
                        intent.putExtra("mp3List", mp3List);
                        mainActivity.startActivity(intent);
                    } else if ("mp4".equalsIgnoreCase(suffix)) {

                    } else {
                    }
                }
            }
        });

        return convertView;
    }

    /**
     * 从传递过来的文件数组中查找出所有的mp3文件的路径，放到ArrayList
     * @param files
     * @return
     */
    private ArrayList<String> getMp3Files(File[] files) {
        ArrayList<String> list = new ArrayList<>();
        for (File file : files) {
            String fileName = file.getName();// aaa.mp3
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);// mp3
            if ("mp3".equalsIgnoreCase(suffix)) {
                list.add(file.getAbsolutePath());
            }
        }

        return list;
    }

    // 更新Adapter中数据
    public void updateData(File file) {
        this.files = file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return !pathname.getName().startsWith(".");
            }
        });
        sortFiles(files);
        // 如果更新了apdater中的数据，需要刷新
        notifyDataSetChanged();
    }

    private void sortFiles(File[] files) {
        Collections.sort(Arrays.asList(files), new Comparator<File>() {

            @Override
            public int compare(File file1, File file2) {
                // 文件夹放在前面，文件放在后边
                if (file1.isDirectory() && file2.isFile()) {
                    return -1;
                } else if (file1.isFile() && file2.isDirectory()) {
                    return 1;
                } else {
                    // file1是文件夹file2也是文件夹或者file1是文件file2是文件
                    // getName得到的是文件名字的字符串，字符串默认实现一个compareTo比较
                    // 按照字母先后顺序
                    return file1.getName().compareTo(file2.getName());
                }
            }
        });
    }

    // 封装list_item里面的对象
    public class ViewHoder{
        public ImageView ivFileIcon;
        public TextView tvFileName;
    }
}
